﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.ComponentModel;

namespace OpenST
{

    public class Bug
    {
        public enum Direction { Left, Right, Up, Down };

        public int PositionX;
        public int PositionY;

#region Properties
        [DescriptionAttribute("Where to start (X Coordinate")]
        public int StartX { get; set; }
        [DescriptionAttribute("Where to start (Y Coordinate")]
        public int StartY { get; set; }
        [DescriptionAttribute("Which Direction to travel in from a reset")]
        public Direction StartDirection { get; set; }
        [DescriptionAttribute("From -1 (1 Octave lower) to 1 (1 Octave higher)")]
        public float StartPitchAdjust { get; set; }
        [DescriptionAttribute("Speed Multiplier, To set this globally, use the composition properties")]
        public float Speed{get;set;}
        [CategoryAttribute("Mixer"), DescriptionAttribute("Volume of this bug, Should be from 0.0 (Silent) to 1.0 (Full Volume)")]
        public float Volume { get; set; }
        [CategoryAttribute("Mixer"), DescriptionAttribute("Stereo placement of this bug, Should be from -1.0 (Full Left) to 1.0 (Full Right)")]
        public float Pan { get; set; }
#endregion

        public float PitchAdjust;
        public Direction CurrentDirection;
        public string Name;
        public string Description;
        public string Author;
        public string BugXML;


        private SoundEffect[] SoundEffects;
        private Composition composition;
        public Texture2D BugSprite;
        public Texture2D BugMenuSprite;

        public float ProgressToNextTile;

        public Bug()
        {
            PositionX = 0;
            PositionY = 0;
            StartX = 0;
            StartY = 0;
            Speed = 1.0f;
            Volume = 0.8f;
            Pan = 0.0f; // Centered
            PitchAdjust = 0.0f; // +1 = Octave Up, -1  Octave Ddown
            StartPitchAdjust = 0.0f;
            CurrentDirection = Direction.Right;
            StartDirection = Direction.Right;
            ProgressToNextTile = 0.0f;
        }

        public void SetComposition(Composition comp) { composition =  comp; }
        public void PlaySound(int soundnum) {
            
            //SoundEffectInstance sei = SoundEffects[soundnum].CreateInstance();
            //sei.IsLooped = false;
            //sei.Pitch = PitchAdjust;
            //sei.Volume = Volume;
            //sei.Pan = Pan;
            //sei.Play();
            SoundEffects[soundnum].Play(Volume, PitchAdjust, Pan);
        }

        public void Reset()
        {
            PositionX = StartX;
            PositionY = StartY;
            CurrentDirection = StartDirection;
            ProgressToNextTile = 0.0f;
            PitchAdjust = StartPitchAdjust;
        }

        public void LoadContent(ContentManager content, string xmlfile)
        {
            
            DataFormats.BugData BugData = content.Load<DataFormats.BugData>(xmlfile);
            BugXML = xmlfile;

            // Load all the sounds listed inside the XML file...
            SoundEffects = new SoundEffect[BugData.Sounds.Length];
            for(int i = 0; i < BugData.Sounds.Length; i++)
            {
                SoundEffects[i] = content.Load<SoundEffect>(BugData.Sounds[i]);
            }

            BugMenuSprite = content.Load<Texture2D>(BugData.MenuSprite);
            BugSprite = content.Load<Texture2D>(BugData.Sprite);

            Name = BugData.Name;
            Author = BugData.Author;
            Description = BugData.Description;      
        }

        public void Update(int Milliseconds)
        {
            ProgressToNextTile += (((float)Milliseconds / 1000) * Speed) * composition.Speed;

            if (ProgressToNextTile >= 1.0f)
            {
                switch (CurrentDirection)
                {
                    case Direction.Right:
                        PositionX += 1;
                        break;
                    case Direction.Left:
                        PositionX -= 1;
                        break;
                    case Direction.Up:
                        PositionY -= 1;
                        break;
                    case Direction.Down:
                        PositionY += 1;
                        break;
                }

                // Flip at bounds
                if (PositionX == 0)
                {
                    if(CurrentDirection == Direction.Left)
                        CurrentDirection = Direction.Right;
                }
                if (PositionX == Composition.CompositionMaxSizeX)
                {
                    if (CurrentDirection == Direction.Right)
                        CurrentDirection = Direction.Left;
                }
                if (PositionY == 0)
                {
                    if (CurrentDirection == Direction.Up)
                        CurrentDirection = Direction.Down;
                }
                if (PositionY == Composition.CompositionMaxSizeY)
                {
                    if (CurrentDirection == Direction.Down)
                        CurrentDirection = Direction.Up;
                }

                if (composition.Tiles[PositionX, PositionY] != null)
                {
                    composition.Tiles[PositionX, PositionY].Process(this);
                }

                ProgressToNextTile = 0.0f;
            }

        }

    }
}
